package com.cierzo.eagleband.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PacketParsing {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String a = "Tcp:           window = 5840" +
				"Tcp:         checksum = 0xDF6F (57199)" +
				"Tcp:           urgent = 0" +
				"Tcp: " +
				"Data:  ******* Payload offset=54 (0x36) length=143 " +
				"Data: " +
				"0036: 2b 4f 4b 20  6c 6c 63 63  34 39 30 2d  61 2e 73 65    +OK llcc490-a.se" +
				"0046: 72 76 69 64  6f 72 65 73  64 6e 73 2e  6e 65 74 20    rvidoresdns.net " +
				"0056: 43 79 72 75  73 20 50 4f  50 33 20 76  32 2e 32 2e    Cyrus POP3 v2.2." +
				"0066: 31 32 2d 49  6e 76 6f 63  61 2d 52 50  4d 2d 32 2e    12-Invoca-RPM-2." +
				"0076: 32 2e 31 32  2d 39 2e 52  48 45 4c 34  20 73 65 72    2.12-9.RHEL4 ser" +
				"0086: 76 65 72 20  72 65 61 64  79 20 3c 31  38 36 32 35    ver ready <18625" +
				"0096: 32 31 39 34  34 2e 31 32  35 35 32 36  38 34 33 33    21944.1255268433" +
				"00a6: 40 6c 6c 63  63 34 39 30  2d 61 2e 73  65 72 76 69    @llcc490-a.servi" +
				"00b6: 64 6f 72 65  73 64 6e 73  2e 6e 65 74  3e 0d 0a      doresdns.net>.. ";
		System.out.println(getData(a)); 
	}
	
	public static String getOrigen(String packet){
		String patternOrigen = "Ip:           source = [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}";
        Pattern pattern = Pattern.compile(patternOrigen);

        Matcher matcher = pattern.matcher(packet.toString());
        boolean matchFound = matcher.find();    
        String match = matcher.group();
        
        return match.replace("Ip:           source = ", "");
	}
	
	public static String getDestino(String packet){
		String patternDestino = "Ip:      destination = [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}";
        Pattern pattern = Pattern.compile(patternDestino);

        Matcher matcher = pattern.matcher(packet.toString());
        boolean matchFound = matcher.find();    
        String match = matcher.group();
        
        return match.replace("Ip:      destination = ", "");
	}
	
	public static String getData(String packet){
		String match = "";
		String patternDestino = "[0-9a-z]{2} [0-9a-z]{2} [0-9a-z]{2} [0-9a-z]{2}    .{16}";
        Pattern pattern = Pattern.compile(patternDestino);

        Matcher matcher = pattern.matcher(packet.toString());
        
        
        while(matcher.find()){
        	//System.out.println(matcher.group());  
        	match = match + matcher.group().substring(15,31);
        }
        return match;
	}
	

}
